package game.Enemty;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 游戏背景类，负责管理游戏场景中的背景、障碍物、奖励和怪物等元素
 * 同时处理角色移动时的背景滚动逻辑和与旗帜的交互检测
 */
public class Background implements Runnable {
    public Image background, flag; // 背景和旗帜的图像资源
    public int x = 0, y = 0; // 背景坐标
    public int flagX = 1600, flagY = 240; // 旗帜位置
    Thread thread = new Thread(this); // 控制背景行为的线程
    public List<Obstacle> obstacles = new ArrayList<>(); // 障碍物列表
    public List<Award> awards = new ArrayList<>(); // 奖励物品列表
    public List<Monster> Monster = new ArrayList<>(); // 怪物列表
    public Catt cat = new Catt(); // 玩家控制的角色
    public boolean end = false; // 是否到达终点标志

    public Background() {
        background = new ImageIcon("img/background/background_2.jpg").getImage();
        flag = new ImageIcon("img/flag/6.png").getImage();
        thread.start();
        init();
    }

    /**
     * 初始化游戏元素
     */
    public void init() {
        iniOb(); // 初始化障碍物
        iniAward(); // 初始化奖励物品
    }

    /**
     * 初始化障碍物
     */
    private void iniOb() {
        // 添加多个不同类型和位置的障碍物
        obstacles.add(new Obstacle(0, 330, 2, new ImageIcon("img/background/hpNull.png").getImage(), 800, 10));
        obstacles.add(new Obstacle(200, 200, 3, new ImageIcon("img/obstacle/LuckyBlock.png").getImage()));
        obstacles.add(new Obstacle(400, 200, 1, new ImageIcon("img/obstacle/brick.png").getImage()));
        obstacles.add(new Obstacle(450, 200, 2, new ImageIcon("img/obstacle/brick2.png").getImage()));
        obstacles.add(new Obstacle(450, 285, 2, new ImageIcon("img/obstacle/brick2.png").getImage()));
        obstacles.add(new Obstacle(500, 200, 3, new ImageIcon("img/obstacle/LuckyBlock.png").getImage()));
        obstacles.add(new Obstacle(550, 200, 3, new ImageIcon("img/obstacle/LuckyBlock.png").getImage()));
        //管道1
        obstacles.add(new Obstacle(700, 230, 4, new ImageIcon("img/obstacle/pipe.png").getImage(), 70, 100));
        obstacles.add(new Obstacle(850, 200, 1, new ImageIcon("img/obstacle/brick.png").getImage()));
        obstacles.add(new Obstacle(900, 200, 1, new ImageIcon("img/obstacle/brick.png").getImage()));
        obstacles.add(new Obstacle(950, 200, 1, new ImageIcon("img/obstacle/brick.png").getImage()));
        obstacles.add(new Obstacle(1000, 200, 2, new ImageIcon("img/obstacle/brick2.png").getImage()));
        obstacles.add(new Obstacle(1050, 200, 1, new ImageIcon("img/obstacle/brick.png").getImage()));
        obstacles.add(new Obstacle(1100, 200, 2, new ImageIcon("img/obstacle/brick2.png").getImage()));
        obstacles.add(new Obstacle(700, 70, 3, new ImageIcon("img/obstacle/LuckyBlock.png").getImage()));
        //管道2
        obstacles.add(new Obstacle(1300, 230, 4, new ImageIcon("img/obstacle/pipe.png").getImage(), 70, 100));
        //梯子


//        obstacles.add(new Obstacle(1400, 285, 2, new ImageIcon("img/obstacle/brick.png").getImage()));
//        obstacles.add(new Obstacle(1450, 285, 2, new ImageIcon("img/obstacle/brick.png").getImage()));
//        obstacles.add(new Obstacle(1450, 235, 2, new ImageIcon("img/obstacle/brick.png").getImage()));
//        obstacles.add(new Obstacle(1500, 285, 2, new ImageIcon("img/obstacle/brick.png").getImage()));
//        obstacles.add(new Obstacle(1500, 235, 2, new ImageIcon("img/obstacle/brick.png").getImage()));
        //obstacles.add(new Obstacle(1500, 185, 2, new ImageIcon("img/obstacle/brick.png").getImage()));


        obstacles.add(new Obstacle(1500, 235, 2, new ImageIcon("img/obstacle/brick.png").getImage()));
        obstacles.add(new Obstacle(1500, 285, 2, new ImageIcon("img/obstacle/brick.png").getImage()));


        //obstacles.add(new Obstacle(1550, 285, 2, new ImageIcon("img/obstacle/brick.png").getImage()));
        //obstacles.add(new Obstacle(1550, 235, 2, new ImageIcon("img/obstacle/brick.png").getImage()));
        //obstacles.add(new Obstacle(1550, 185, 2, new ImageIcon("img/obstacle/brick.png").getImage()));


        obstacles.add(new Obstacle(1550, 135, 2, new ImageIcon("img/obstacle/brick.png").getImage()));
        obstacles.add(new Obstacle(1600, 135, 2, new ImageIcon("img/obstacle/brick.png").getImage()));

        //门
        obstacles.add(new Obstacle(1750, 285, 2, new ImageIcon("img/obstacle/brick.png").getImage()));
        obstacles.add(new Obstacle(1750, 235, 2, new ImageIcon("img/obstacle/brick.png").getImage()));
        obstacles.add(new Obstacle(1750, 185, 2, new ImageIcon("img/obstacle/brick.png").getImage()));
        obstacles.add(new Obstacle(1750, 135, 2, new ImageIcon("img/obstacle/brick.png").getImage()));
        obstacles.add(new Obstacle(1800, 135, 2, new ImageIcon("img/obstacle/brick2.png").getImage()));
        obstacles.add(new Obstacle(1850, 285, 2, new ImageIcon("img/obstacle/brick.png").getImage()));
        obstacles.add(new Obstacle(1850, 235, 2, new ImageIcon("img/obstacle/brick.png").getImage()));
        obstacles.add(new Obstacle(1850, 185, 2, new ImageIcon("img/obstacle/brick.png").getImage()));
        obstacles.add(new Obstacle(1850, 135, 2, new ImageIcon("img/obstacle/brick.png").getImage()));
    }

    /**
     * 根据障碍物初始化奖励物品
     */
    private void iniAward() {
        for (int i = 0; i < obstacles.size(); i++) {
            Obstacle obstacle = obstacles.get(i);
            if (obstacle.type == 3) {
                // 当障碍物类型为 LuckyBlock（type=3）时生成奖励物品
                awards.add(new Award(obstacle.y + 5, obstacle.x + 5, 1, obstacle));
            }
        }
    }

    /**
     * 将玩家角色绑定到背景中，用于同步移动
     * @param catt 玩家控制的角色对象
     */
    public void move(Catt catt) {
        cat = catt;
    }


    @Override
    public void run() {
        while (true) {
            if (cat.x > 650 && x >= -1100 ) {
                // 当角色向右移动且超出一定范围时，背景左移以实现滚动效果
                x -= 550;
                cat.x -= 550;
                flagX -= 550;
                for (int i = 0; i < awards.size(); i++) {
                    awards.get(i).x -= 550;
                }
                for (int i = 1; i < obstacles.size(); i++) {
                    obstacles.get(i).x -= 550;
                }

                for (int i = 0; i < Monster.size(); i++) {
                    Monster.get(i).x -= 500;
                }

            }
            touchFlga();
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 检测角色是否触达旗帜
     */
    private void touchFlga() {
        if (cat.x >= flagX) {
            // 角色到达旗帜位置后标记为完成
            end = true;
        }
    }
}